package cn.itcast.travel.dao.impl;

import cn.itcast.travel.dao.UserDao;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
@SuppressWarnings("all")
public class UserDaoImpl implements UserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    public User findUserByUserName(String userName) {
        User user = null;
        try {
            String sql = "select * from table_user where userName = ?";
            user=template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),userName);
        } catch (Exception e) {

        }
        return user;
    }
    public void save(User user) {
        String sql = "insert into tab_user (username,password,name,birthday,sex,telephone,email,status,code) values(?,?,?,?,?,?,?,?,?)";
        template.update(sql,
                user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getBirthday(),
                user.getSex(),
                user.getTelephone(),
                user.getEmail(),
                user.getStatus(),
                user.getCode());

    }

    @Override
    public User finByCode(String code) {
        User user=null;
        try {
            String sql = "select * from tab_user where code = ?";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), code);
        } catch (DataAccessException e) {
            throw new RuntimeException(e);
        }

        return user;
    }

    @Override
    public void updateStatus(User user) {
        String sql = "update tab_user set status='Y' where uid=?";
        template.update(sql,user.getUid());
    }

    @Override
    public User findByUserNameAndPassWord(String userName, String password) {
        User user = null;
        try {
            String sql = "select * from tab_user where userName = ? and password = ?";
            user=template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),userName,password);
        } catch (Exception e) {

        }
        return user;
    }
}
